package com.ea.eadp.nexus.proxy.tests.identity;

import com.ea.eadp.nexus.test.common.AccessToken;
import com.ea.eadp.nexus.test.common.ApiClients.NexusConnectApi;
import com.ea.eadp.nexus.test.common.ApiClients.NexusGatewayApi;
import com.ea.eadp.nexus.test.common.ApiClients.NucleusApi;
import com.ea.eadp.nexus.test.common.NexusGatewayApiEnum;
import com.ea.eadp.nexus.test.common.TestUtil;
import com.ea.nucleus.caseproperties.CaseProperties;
import com.ea.nucleus.caseproperties.Priority;
import com.ea.nucleus.caseproperties.Status;
import com.ea.nucleus.eaid.pid.jaxb.PidType;
import com.ea.nucleus.eaid.pid.jaxb.PidUri;
import com.ea.nucleus.eaidpid.integration.client.pids.PostPidRequest;
import com.ea.nucleus.eaidpid.integration.client.pids.PostPidService;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.HashMap;


@CaseProperties(
        //release = Release.Nov2015,
        //features = Oct2015.GOPFR_2435,
        owner = "ChrisShen"
)
public class SecondChanceOptin {
    private static String OPTIN_CLIENT1 = "newconsole-scoi-test-client1";
    private static String OPTIN_CLIENT2 = "newconsole-scoi-test-client2";
    private static String password = "1234qwER";

    @CaseProperties(
            priority = Priority.BVT,
            status = Status.Enable
    )
    @Test(groups = {"regression", "intregression"})
    public void testSecondChanceOptinGateway() throws Exception {
        String email = TestUtil.getRandomEmail();
        Long pidID = postPidOnPreviousDate(email);
        String originId = TestUtil.getRandomOriginId();
        NucleusApi.instance().postPersona(pidID, originId);

        HashMap<String, String> context = new HashMap<>();
        context.put("pid", pidID.toString());
        AccessToken token = NexusConnectApi.instance().getTokenByUserPwdWithClientSecret(email, password, OPTIN_CLIENT1, "secret", null);

        String response = NexusGatewayApi.instance().get(token, NexusGatewayApiEnum.pid_second_chance_optin_get, context);
        Assert.assertTrue(response.contains("NO"));
        response = NexusGatewayApi.instance().get(token, NexusGatewayApiEnum.pid_second_chance_optin_get, context);
        Assert.assertTrue(response.contains("NO"));
        response = NexusGatewayApi.instance().get(token, NexusGatewayApiEnum.pid_second_chance_optin_get, context);
        Assert.assertTrue(response.contains("YES"));
    }

    @CaseProperties(
            priority = Priority.Dailies,
            status = Status.Enable
    )
    @Test(groups = {"regression", "intregression"})
    public void testSecondChanceOptinGatewayScopeForbidden() throws Exception {
        String email = TestUtil.getRandomEmail();
        Long pidID = postPidOnPreviousDate(email);
        String originId = TestUtil.getRandomOriginId();
        NucleusApi.instance().postPersona(pidID, originId);

        HashMap<String, String> context = new HashMap<>();
        context.put("pid", pidID.toString());
        AccessToken token = NexusConnectApi.instance().getTokenByUserPwdWithClientSecret(email, password, OPTIN_CLIENT2, "secret", null);
        try {
            NexusGatewayApi.instance().get(token, NexusGatewayApiEnum.pid_second_chance_optin_get, context);
            Assert.fail("Exception not thrown");
        } catch (Exception ex) {
            Assert.assertTrue(ex.getMessage().contains("scope_forbidden"), "the error is incorrect");
        }
    }

    private Long postPidOnPreviousDate(String email) throws Exception {
        PostPidRequest postPidRequest = new PostPidRequest();
        PidType pid = new PidType();
        pid.setDob("1990-12-12");
        pid.setCountry("US");
        pid.setGlobalOptin("false");
        pid.setPassword("1234qwER");
        pid.setStrength("STRONG");
        pid.setThirdPartyOptin("false");
        pid.setStatus("ACTIVE");
        pid.setEmail(email);
        pid.setTosVersion("666");
        pid.setLanguage("en");
        postPidRequest.setRequestData(pid);
        postPidRequest.addHeader("X-QA-CREATE-DATE", getPreviousDate());

        PidUri pidUri = PostPidService.instance().post(postPidRequest);
        String split[] = pidUri.getPidUri().split("/");
        return Long.parseLong(split[split.length - 1]);
    }

    private String getPreviousDate() {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        GregorianCalendar now = new GregorianCalendar();
        now.add(GregorianCalendar.MONTH, -2);
        return format.format(now.getTime());
    }
}
